﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity.Infrastructure;
using BusinessLogicLayer;
using DataAccessLayer;
using MyException;

namespace Stationery_Store_Inventory_Management
{
    public partial class ItemWiseDeptList : System.Web.UI.Page
    {
        StoreRetrievalController sCon = new StoreRetrievalController();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 5)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                String ItemCode = Request.QueryString["ic"];
            }
            catch (Exception ex)
            {
            }
        }

        public IEnumerable<Object> allocationGrid_GetData()
        {
            String ItemCode = Request.QueryString["ic"];
            var queryResult = sCon.getItemWiseDeptRequestsCont(ItemCode);
            return queryResult;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int total = 0;
            //int actualQty = 0;
            List<int> lstDeptReqDetId = new List<int>();
            List<int> lstAllocatedQty = new List<int>();

            try
            {
                for (int i = 0; i < allocationGrid.Rows.Count; i++)
                {
                    TextBox txtActualQty = (TextBox)allocationGrid.Rows[i].FindControl("ActualQty");
                    total += Convert.ToInt32(txtActualQty.Text);
                    lstAllocatedQty.Add(Convert.ToInt32(txtActualQty.Text));
                    lstDeptReqDetId.Add(Convert.ToInt32(allocationGrid.Rows[i].Cells[1].Text));

                    if (Convert.ToInt32(txtActualQty.Text) > Convert.ToInt32(allocationGrid.Rows[i].Cells[3].Text))
                    {
                        lblError.Visible = true;
                        throw new AllocateQtyMoreThanRequestedQtyException();
                    }
                }
                if (total > Convert.ToInt32(allocationGrid.Rows[0].Cells[4].Text))
                {
                    lblError.Visible = true;
                    throw new TotalAllocatedQtyMoreThanAvailableQtyException();
                }
                else
                {
                    sCon.updateDeptReqDetActualQtyCont(lstDeptReqDetId, lstAllocatedQty);
                    Button1.Visible = false;
                    btnBack.Visible = true;
                    lblError.Text = ("Allocate Quantity has been updated. Please click the Back Button to return to the retrieval list.");
                }
            }
            catch (AllocateQtyMoreThanRequestedQtyException ex)
            {
                lblError.Text = "Allocate Quantity cannot be more than Requested Quantity";
            }
            catch (TotalAllocatedQtyMoreThanAvailableQtyException ex)
            {
                lblError.Text = "Total Allocate Quantity must be equal to the Available Quantity";
            }
            catch (Exception ex)
            {
            }   
        }

        protected void allocationGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "btnAllocate")
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    TextBox ActualQty = (TextBox)allocationGrid.Rows[index].Cells[1].FindControl("ActualQty");
                    string drid = allocationGrid.Rows[index].Cells[2].Text;
                    string avaqty = allocationGrid.Rows[index].Cells[5].Text;
                    int dReqId = Convert.ToInt32(drid);
                    int ac = Convert.ToInt32(ActualQty.Text);
                    int QtyAvailable = Convert.ToInt32(avaqty);
                    if (ac > QtyAvailable)
                    {
                        Response.Write("Cannot allocate more than that is available");
                    }
                    else
                    {
                        sCon.allocateActualQtyCont(dReqId, ac);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("StoreRetrievalList.aspx");
            }
            catch (Exception ex)
            {
            }
        }

    }
}